package com.grj.parkingsystem.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.grj.parkingsystem.common.Result;
import com.grj.parkingsystem.entity.SystemConfig;
import com.grj.parkingsystem.service.SystemConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/config")
@Api(tags = "7. 系统配置管理")
public class SystemConfigController {

    @Autowired
    private SystemConfigService systemConfigService;

    @GetMapping("/all")
    @ApiOperation(value = "获取所有配置", notes = "获取系统所有配置项")
    @ApiOperationSupport(order = 1)
    public Result<?> getAllConfigs() {
        Map<String, String> configs = systemConfigService.getAllConfigs();
        return Result.success(configs);
    }

    @GetMapping("/{configKey}")
    @ApiOperation(value = "获取单个配置", notes = "获取指定配置项的值")
    @ApiOperationSupport(order = 2)
    public Result<?> getConfig(@PathVariable String configKey) {
        Object value = systemConfigService.getConfigValue(configKey);
        return Result.success(value);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新配置", notes = "更新系统配置项（需要管理员权限）")
    @ApiOperationSupport(order = 3)
    public Result<?> updateConfig(@ApiParam(value = "认证Token", required = true)
                                @RequestBody SystemConfig config) {
        systemConfigService.updateById(config);
        return Result.success("更新成功");
    }
}